package cn.itcast.order.service;

import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.Order;
import cn.itcast.feign.pojo.User;

import cn.itcast.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserClient userClient;
   /* @Autowired
    private RestTemplate restTemplate;*/

    /*public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 4.返回
        return order;
    }*/

    /*//实现远程调用
    public Order queryOrderById(Long orderId) {
        Order order = orderMapper.findById(orderId);
        //远程查询用户，地址
        String url = "http://localhost:8081/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }*/

    //实现拉取服务并负载均衡
   /* public Order queryOrderById(Long orderId) {
        Order order = orderMapper.findById(orderId);
        //远程查询用户，地址
        String url = "http://userservice/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }*/

    public Order queryOrderById(Long orderId) {
        Order order = orderMapper.findById(orderId);
        //远程查询用户，地址
        String url = "http://userservice/user/"+order.getUserId();
        //User user = restTemplate.getForObject(url, User.class);
        User user = userClient.selectById(order.getUserId());
        order.setUser(user);
        return order;
    }
}
